package com.hnfnu.service.impl;

import com.hnfnu.protocol.dto.*;
import com.hnfnu.entity.Donations;
import com.hnfnu.protocol.vo.NumberOfDonorsVO;
import com.hnfnu.service.BaseService;
import com.hnfnu.service.DonationsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DonationsServiceImpl extends BaseService implements DonationsService{

    @Override
    public boolean save(Donations donations) {
        return donationsMapper.insert(donations);
    }

    @Override
    public List<DonationsPageDTO> getDonationsPage() {
        return donationsMapper.page();
    }

    @Override
    public List<DonationsIntervalDTO> getIntervalNUmber() {
        List<DonationsIntervalDTO> intervalNUmber = donationsMapper.getIntervalNUmber();
        for (DonationsIntervalDTO o : intervalNUmber) {
            String amountLevel = o.getAmountLevel();
            o.setAmountLevel(amountLevel.substring(2));
        }
        return intervalNUmber;
    }

    @Override
    public List<DonationsProvinceDTO> getProvinceNumber() {
        return donationsMapper.getProvinceNumber();
    }

    @Override
    public List<DonationsMonthTrendDTO> getMonthTrend() {
        return donationsMapper.getMonthTrend();
    }

    @Override
    public Integer getMaleNumber() {
        return donationsMapper.getMaleNumber();
    }

    @Override
    public List<DonationsProjectNameAmountsDTO> getProjectAmounts() {
        List<DonationsProjectIdAmountsDTO> projectIdAmountsDTOS = donationsMapper.getProjectIdAmounts();
        List<DonationsProjectNameAmountsDTO> projectNameAmountsDTOS = new ArrayList<>();
        for (DonationsProjectIdAmountsDTO projectIdAmountsDTO : projectIdAmountsDTOS) {
            Integer projectId = projectIdAmountsDTO.getProjectId();
            String projectName = projectMapper.getNameById(projectId);
            Double amounts = projectIdAmountsDTO.getAmounts();
            DonationsProjectNameAmountsDTO projectNameAmountsDTO = new DonationsProjectNameAmountsDTO(projectName, amounts);
            projectNameAmountsDTOS.add(projectNameAmountsDTO);
        }
        return projectNameAmountsDTOS;
    }

    @Override
    public Double getTotalAmount() {
        return donationsMapper.getTotalAmount();
    }

    @Override
    public List<DonationsSatisfactionDTO> getSatisfaction() {
        return donationsMapper.getSatisfaction();
    }

    @Override
    public boolean addSatisfaction(Integer grade) {
        return donationsMapper.addSatisfaction(grade);
    }

    @Override
    public List<DonationsPurposeDTO> getPurposeTotalAmount() {
        return donationsMapper.getPurposeTotalAmount();
    }

    @Override
    public List<DonationsPurposeDetailPercentDTO> getPurposeDetailPercent() {
        List<DonationsPurposeDetailAmountDTO> purposeDetailAmount = donationsMapper.getPurposeDetailAmount();
        double totalAmount1 = 0.0d;
        double totalAmount2 = 0.0d;
        double totalAmount3 = 0.0d;
        for (DonationsPurposeDetailAmountDTO o : purposeDetailAmount) {
            if (o.getPurposeId() == 1) {
                totalAmount1 += o.getTotalAmount();
            }
            if (o.getPurposeId() == 2) {
                totalAmount2 += o.getTotalAmount();
            }
            if (o.getPurposeId() == 3) {
                totalAmount3 += o.getTotalAmount();
            }
        }

        List<DonationsPurposeDetailPercentDTO> purposeDetailPercentDTOS = new ArrayList<>();
        for (DonationsPurposeDetailAmountDTO o : purposeDetailAmount) {
            DonationsPurposeDetailPercentDTO purposeDetailPercentDTO = new DonationsPurposeDetailPercentDTO();
            purposeDetailPercentDTO.setName(o.getName());
            if (o.getPurposeId() == 1) {
                if (totalAmount1 == 0) {
                    purposeDetailPercentDTO.setPercent(0.0d);
                }
                purposeDetailPercentDTO.setPercent(o.getTotalAmount() / totalAmount1);
            }
            if (o.getPurposeId() == 2) {
                if (totalAmount1 == 0) {
                    purposeDetailPercentDTO.setPercent(0.0d);
                }
                purposeDetailPercentDTO.setPercent(o.getTotalAmount() / totalAmount2);
            }
            if (o.getPurposeId() == 3) {
                if (totalAmount1 == 0) {
                    purposeDetailPercentDTO.setPercent(0.0d);
                }
                purposeDetailPercentDTO.setPercent(o.getTotalAmount() / totalAmount3);
            }
            purposeDetailPercentDTOS.add(purposeDetailPercentDTO);

        }
        return purposeDetailPercentDTOS;
    }

    @Override
    public NumberOfDonorsVO getNumberOfDonors(Integer projectId) {
        return donationsMapper.getNumberOfDonors(projectId);
    }


    @Override
    public String getUserNameByUserId(Integer userId) {
        return userMapper.getNameByid(userId);
    }

    @Override
    public Donations getByTrade(String tradeNo) {
        return donationsMapper.getDonationByTrade(tradeNo);
    }

    @Override
    public void updateStatus(String tradeNo) {

        donationsMapper.updateStatus(tradeNo);
    }

    @Override
    public Double getRaised(Integer projectId) {
        return donationsMapper.getRaised(projectId);
    }

    @Override
    public List<DonationsOrderByTimeDTO> selectAmountAndUserIdOrderByTime(Integer projectId) {
        return donationsMapper.selectAmountAndUserIdOrderByTime(projectId);
    }

    @Override
    public void updateSpendByUserIdAndProjectId(Integer userId, Integer projectId) {
        donationsMapper.updateSpendByUserIdAndProjectId(userId, projectId);
    }

    @Override
    public int getFemaleNumber() {
        return donationsMapper.getFeMaleNumber();
    }

    @Override
    public int getAgeInvalNumber(int l, int r) {
        return donationsMapper.getAgeInvalNumber(l, r);
    }

    @Override
    public int getTotalNumberOfUsers() {
        return donationsMapper.getTotalNumberOfUsers();
    }

    @Override
    public int getProjectStageNumber(String step) {
        if(step == "start")
            return donationsMapper.getStartStep();
        else if(step == "middle")
            return donationsMapper.getMiddleStep();
        else if(step == "end")
            return donationsMapper.getEndStep();
        else
            return -1;
    }

    @Override
    public double getWeekDonationNum() {
        return donationsMapper.getWeekDonationNum();
    }
}
